<?php

namespace App\Models\Panx;

use App\Models\Customer\Customer;
use App\Observers\BosCleanObserver;
use App\Services\Utils\ActionLogger;
use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * @property string logo
 * @property string name
 * @property int customer_id
 * @property int id
 * @property bool locked
 * @property string|null url
 *
 * @property-read Customer $customer
 * @property-read Collection|Product[] $products
 * @property-read Collection|Product[] $onShelfProducts
 * @property-read Collection|BrandSeries[] $brandSeries
 * @property-read Collection|BrandSeries[] $brandSeriesBySortLatest - 根据 sort 升序与 created_at 倒序的关联系列
 *
 * @property Collection|BrandSeries[] brand_series - 同 brandSeries
 *
 * @mixin Builder
 */
class Brand extends Model
{
    use Bosable;
    use ActionLogger;

    /**
     * CUSTOMER_ID 为 0 是 PANX 上传的品牌
     */
    const CUSTOMER_ID_PANX = 0;

    protected static $all_cache;

    protected $brand_bos_key_prefix = 'brand/logo/';

    protected $table = 'brands';

    protected $guarded = [];

    public $bos_columns = ['logo'];

    protected $casts = [
        'customer_id' => 'int',
        'locked'      => 'bool',
    ];

    public static function mapIDtoName(int $id)
    {
        if (! self::$all_cache) {
            self::$all_cache = self::all();
        };

        return self::$all_cache->find($id)->name ?? null;
    }

    /**
     * 注册删除 bosKey 事件绑定
     */
    protected static function boot()
    {
        parent::boot();

        /*
         * 监听bos清理事件
         */
//        self::observe(BosCleanObserver::class);
    }

    /**
     * @param $logo
     * @throws \Exception
     */
    public function setLogoAttribute($logo)
    {
        if (is_file($logo)) {
            $this->attributes['logo'] = $this->uploadPostFile($logo, $this->brand_bos_key_prefix);
        } elseif (is_string($logo)) {
            $this->attributes['logo'] = $logo;
        }
    }

    /**
     * @return mixed|null
     */
    public function getLogoAttribute()
    {
        if (! $this->attributes['logo']) return null;

        return $this->getStaticUrl($this->attributes['logo']);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function products()
    {
        return $this->hasMany(Product::class, 'brand_id', 'id');
    }

    /**
     * @used-by $onShelfProducts - 有上架的商品
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function onShelfProducts()
    {
        $prod = new Product;

        return $this
            ->hasMany(Product::class, 'brand_id', 'id')
            ->where($prod->qualifyColumn('is_sale'), Product::IS_SALE)
            ->where($prod->qualifyColumn('is_deleted'), Product::IS_NO_DELETE);
    }

    /**
     * @used-by $brandSeries
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function brandSeries()
    {
        return $this->hasMany(BrandSeries::class, 'brand_id', 'id');
    }

    /**
     * @used-by $brandSeriesBySortLatest
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function brandSeriesBySortLatest()
    {
        return $this->hasMany(BrandSeries::class, 'brand_id', 'id')
            ->orderBy('sort', 'asc')
            ->orderBy('created_at', 'desc')
        ;
    }

    /**
     * @used-by $customer
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function customer()
    {
        return $this->belongsTo(Customer::class, 'customer_id', 'id');
    }
}
